package Experiment6;
import javax.imageio.stream.FileImageOutputStream;
import java.io.*;
import java.util.*;

public class MemoManager {
    public static boolean SaveMemos(ArrayList<Memo> box,String filename){
        try {
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(filename));
            Iterator<Memo> i = box.iterator();
            while(i.hasNext()){
                os.writeObject(i.next());
            }
        }
        catch (IOException ex){
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    public static boolean ReadMemos(ArrayList<Memo> box,String filename){
        Memo m1 = new Memo();
        try {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(filename));
            BufferedInputStream bs = new BufferedInputStream(new FileInputStream(filename));
            while(is.readObject()!=null){
                m1 = (Memo) is.readObject();
                box.add(m1);
                is.close();
                bs.close();
            }
        }
        catch (EOFException e){
            return false;
        }
        catch (ClassNotFoundException ex){
            ex.printStackTrace();
        }
        catch (IOException ex)
            {
            ex.printStackTrace();
        }
        finally {

        }
        return true;
    }
public static boolean SaveMemos2(ArrayList<Memo> box, String filename){
    try{
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(filename));
        Iterator<Memo> i = box.iterator();
        while(i.hasNext()){
            os.writeObject(i.next());
        }
    }catch(Exception ex){
        ex.printStackTrace();
        return false;
    }
    return true;
}
public static boolean Readmemos(ArrayList<Memo> box, String filename){
        Memo m = new Memo();
        try{
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(filename));
            while(is.readBoolean()){
                m = (Memo)is.readObject();
                box.add(m);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
}
}
